package com.sx.micowell.framework.model.smartDevice;

import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.mvvm.base.data.MutableLiveListData;
import com.example.mvvm.base.data.SingleLiveEvent;
import com.example.mvvm.base.vm.BaseViewModel;
import com.example.mvvm.until.LogUtils;
import com.example.mvvm.until.SPUtils;
import com.example.mvvm.until.ToastUtils;
import com.sx.micowell.framework.app.SPKey;
import com.sx.micowell.framework.arouth.RouteManager;
import com.sx.micowell.framework.binding.BindingCommand;
import com.sx.micowell.framework.binding.impl.IBindingAction;
import com.tuya.smart.android.ble.api.BleConfigType;
import com.tuya.smart.android.ble.api.LeScanSetting;
import com.tuya.smart.android.ble.api.ScanDeviceBean;
import com.tuya.smart.android.ble.api.ScanType;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.home.sdk.bean.ConfigProductInfoBean;
import com.tuya.smart.sdk.api.IBleActivatorListener;
import com.tuya.smart.sdk.api.ITuyaDataCallback;
import com.tuya.smart.sdk.bean.BleActivatorBean;
import com.tuya.smart.sdk.bean.DeviceBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YY
 * @Time: 2021/9/16
 * @Describe TODO
 */
public class SearchDeviceModel extends BaseViewModel {


    private BleActivatorBean bleActivatorBean;

    private final List<ScanDeviceBean> scanDeviceBeanList = new ArrayList<>();

    private final List<ConfigProductInfoBean> infoBeanList = new ArrayList<>();

    public MutableLiveListData<ConfigProductInfoBean> infoBeanListData = new MutableLiveListData<>();

    public SingleLiveEvent<Boolean> loadingEvent = new SingleLiveEvent<>();

    public SingleLiveEvent<Boolean> matchStateEvent = new SingleLiveEvent<>();

    /**
     * 开始扫描
     */
    public void startScan() {
        loadingEvent.setValue(true);

        LeScanSetting scanSetting = new LeScanSetting.Builder()
                .setTimeout(60 * 1000)
                .addScanType(ScanType.SINGLE)
                .build();

        TuyaHomeSdk.getBleOperator().startLeScan(scanSetting, bean -> {
            LogUtils.d("扫描结果:" + bean.getDeviceType());
            loadingEvent.setValue(false);
            scanDeviceBeanList.add(bean);
            getDeviceInfo(bean);
        });
    }

    /**
     * 查询设备名称
     */
    public void getDeviceInfo(ScanDeviceBean scanDeviceBean) {
        TuyaHomeSdk.getActivatorInstance().getActivatorDeviceInfo(scanDeviceBean.getProductId(),
                scanDeviceBean.getUuid(),
                scanDeviceBean.getMac(),
                new ITuyaDataCallback<ConfigProductInfoBean>() {
                    @Override
                    public void onSuccess(ConfigProductInfoBean result) {
                        LogUtils.d("getDeviceInfoSuccess:" + result.getName());
                        infoBeanList.add(result);
                        infoBeanListData.setValue(infoBeanList);
                    }

                    @Override
                    public void onError(String errorCode, String errorMessage) {
                        LogUtils.d("getDeviceInfoError:" + errorMessage);

                    }
                });
    }

    /**
     * 判断 单模或双模设备
     */
    public void startActivator(int pos) {
        String type = scanDeviceBeanList.get(pos).getConfigType();
        LogUtils.d("startActivator :" + type);
        if (BleConfigType.CONFIG_TYPE_SINGLE.getType().equals(type)) {
            singleActivator(pos);
        } else {
            ToastUtils.showLong( "Device Type not support", Toast.LENGTH_SHORT);
        }
    }

    /**
     * 单模设备配网
     */
    private void singleActivator(int pos) {
        LogUtils.d("singleActivator");
        bleActivatorBean = new BleActivatorBean();
        // homeId
        bleActivatorBean.homeId = SPUtils.getInstance().getLong(SPKey.SP_DEVICE_HOME_ID);
        bleActivatorBean.address = scanDeviceBeanList.get(pos).getAddress();
        bleActivatorBean.deviceType = scanDeviceBeanList.get(pos).getDeviceType();
        bleActivatorBean.uuid = scanDeviceBeanList.get(pos).getUuid(); // UUID
        bleActivatorBean.productId = scanDeviceBeanList.get(pos).getProductId();

        TuyaHomeSdk.getActivator().newBleActivator().startActivator(bleActivatorBean,
                                                        new IBleActivatorListener() {
            @Override
            public void onSuccess(DeviceBean deviceBean) {
                bleActivatorBean = null;
                matchStateEvent.setValue(true);
            }

            @Override
            public void onFailure(int code, String msg, Object handle) {
                bleActivatorBean = null;
                matchStateEvent.setValue(false);

            }
        });
    }

    /**
     * 停止扫描
     */
    public void stopScan() {
        TuyaHomeSdk.getBleOperator().stopLeScan();
    }
}
